//
//  MYSQLiteTableiable.swift
//  MYModule
//
//  Created by HFY on 2022/11/6.
//

import UIKit
import SwiftDate
import SQLite


public protocol MYSQLiteTableiabe: SQLiteTableiabe {
    
    /// mac 地址
    var adress: String { get }
    
    /// 本地日期 忽略 UTC 标记
    var localDate: Date { get }
    
    /// 可选 年
    var year: Int { get }
    
    /// 可选 月
    var month: Int { get }
    
    /// 可选 天
    var day: Int { get }
    
  
    
}

public extension MYSQLiteTableiabe {
    
    
    var expression_mac: Expression<String> {
        return Self.expression_mac
    }
    
    
    var localDate: Date {
        let localDateString = date.toString(.custom("yyyy-MM-dd"))
        let arr = localDateString.components(separatedBy: "-")
        let localDate = Date(year: Int(arr[0])!, month: Int(arr[1])!, day: Int(arr[2])!, hour: 0, minute: 0, second: 0)
        return localDate
    }
    
    /// 年
    var year: Int {
        return localDate.year
    }

    /// 月
    var month: Int {
        return localDate.month
    }

    /// 天
    var day: Int {
        return localDate.day
    }


}

//MARK: - static SQL Table keys
public extension MYSQLiteTableiabe {
    
    static var expression_mac: Expression<String> {
       return Expression<String>("mac")
    }
    
    static var expression_localDate: Expression<Date?> {
         return Expression<Date?>("locaDate")
     }
    
    /// 年
   static var expression_year: Expression<Int?> {
        return Expression<Int?>("year")
    }

    /// 月
    static var expression_month: Expression<Int?> {
        return Expression<Int?>("month")
    }

    /// 天
    static var expression_day: Expression<Int?> {
        return Expression<Int?>("day")
    }



}

//MARK: - SQL Table keys
public extension MYSQLiteTableiabe {
    
    var expression_localDate: Expression<Date?> {
        return Self.expression_localDate
    }
    
    /// 年
   var expression_year: Expression<Int?> {
       return Self.expression_year
    }

    /// 月
    var expression_month: Expression<Int?> {
       return Self.expression_month
    }

    /// 天
    var expression_day: Expression<Int?> {
        return Self.expression_day
    }


}


public extension Date {
    
    func sqlDate(region: Region = SwiftDate.defaultRegion) -> Date {
        
        let dateStr = self.toString(.custom("yyyy-MM-dd HH:mm:ss"))
        guard let date = dateStr.toDate(region: region)?.date else {
            return self
        }
        
        return date
    }
}






